package it.rikesh.aip2011.dataloading;

import it.rikesh.aip2011.shared.model.Activity;
import it.rikesh.aip2011.shared.model.Hut;
import it.rikesh.aip2011.shared.model.HutActivity;
import it.rikesh.aip2011.shared.model.Trail;

import java.io.InputStream;
import java.util.Stack;

import javax.servlet.ServletContext;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.slim3.datastore.Datastore;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

/**
 * @author  RN
 */
public class ActivityHandler extends DefaultHandler{
    private Stack<Activity> activityStack;
    private String characters;
    private String[] huts;
    private String trail;
    
    public ActivityHandler(ServletContext context){
        SAXParserFactory factory2 = SAXParserFactory.newInstance();
        try {
            SAXParser saxParser = factory2.newSAXParser();
            InputStream is = context.getResourceAsStream("/data/activity.xml");
            saxParser.parse(is, this);
        } catch (Throwable t) {

            t.printStackTrace();

        }
    }
    
    public void startDocument() 
    throws SAXException {

        activityStack = new Stack<Activity>();

    }

    public void startElement(String namespaceURI, 
            String localName,
            String qualifiedName,
            Attributes attributes) 
    throws SAXException {

        if (qualifiedName.equals("activity")) {

            Activity activity = new Activity();
            Key key = Datastore.createKey(Activity.class, Long.parseLong(attributes.getValue("id")));
            activity.setKey(key);
            activityStack.push(activity);

        }

    }

    public void endElement(String namespaceURI, 
            String simpleName,
            String qualifiedName) 
    throws SAXException {

        if (!activityStack.isEmpty()) {

            if (qualifiedName.equals("activity")) {
                Activity ac=activityStack.pop();
                Key key=Datastore.createKey(Trail.class, Long.parseLong(trail));
                Trail trail=Datastore.get(Trail.class, key);
                ac.getTrailRef().setModel(trail);
                Datastore.put(ac);
                Hut hutter;
                for (String hut : huts) {
                    key=Datastore.createKey(Hut.class, Long.parseLong(hut));
                    hutter = Datastore.get(Hut.class, key);
                    HutActivity hc=new HutActivity();
                    hc.getHutRef().setModel(hutter);
                    hc.getActivityRef().setModel(ac);
                    Datastore.put(hc);
                }
                
                
            } 
            else if (qualifiedName.equals("name")) {

                Activity activity = activityStack.pop();
                activity.setName(characters);
                activityStack.push(activity);

            } 
            else if (qualifiedName.equals("title")) {

                Activity activity = activityStack.pop();
                activity.setTitle(characters);
                activityStack.push(activity);

            } 
            else if (qualifiedName.equals("type")) {

                Activity activity = activityStack.pop();
                activity.setType(characters);
                activityStack.push(activity);

            }
            else if (qualifiedName.equals("detail")) {

                Activity activity = activityStack.pop();
                activity.setDetail(new Text(characters));
                activityStack.push(activity);

            }
            else if (qualifiedName.equals("img")) {

                Activity activity = activityStack.pop();
                activity.setImg(characters);
                activityStack.push(activity);

            }
            else if (qualifiedName.equals("equip")) {

                Activity activity = activityStack.pop();
                activity.setEquip(characters);
                activityStack.push(activity);

            }
            else if (qualifiedName.equals("trail")) {

                trail=characters;

            }
            else if (qualifiedName.equals("huts")) {

                huts=characters.split(";");

            }
        }

    }

    public void characters(char buf[], int offset, int len) 
    throws SAXException {

        characters = new String(buf, offset, len);

    }
}
